package com.ebc.eservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ebc.eservice.mapper.CardMapper;
import com.ebc.eservice.mapper.CompanySectorMapper;
import com.ebc.eservice.pojo.EbcCard;
import com.ebc.eservice.pojo.EbcCompanySector;
import com.ebc.eservice.pojo.dto.MenuItem;
import com.ebc.eservice.service.EbcCompanySectorService;
import com.ebc.eservice.util.JsonResult;
import com.ebc.eservice.vo.sector.ChildSectors;
import com.ebc.eservice.vo.sector.SectorInfoReq;
import com.ebc.eservice.vo.sector.SectorListVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: Joey
 * @Description: 公司部门信息
 * @date:2022/10/5 22:31
 */
@Service
public class SectorImp implements EbcCompanySectorService {

    @Autowired
    private CompanySectorMapper companySectorMapper;
    @Autowired
    private CardMapper cardMapper;

    @Override
    public JsonResult getSectorByCompany(Integer companyId) {
        QueryWrapper<EbcCompanySector> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("company_id",companyId);
        //所有信息
        List<EbcCompanySector> allSectors = companySectorMapper.selectList(queryWrapper);

        QueryWrapper<EbcCompanySector> queryParentWrapper = new QueryWrapper<>();
        queryParentWrapper.eq("company_id",companyId);
        queryParentWrapper.eq("pid",0);
        //父级部门信息
        List<EbcCompanySector> sectorParentList = companySectorMapper.selectList(queryParentWrapper);

        List<SectorListVo> SectorList = new ArrayList<>();
//        for (int s = 1; s < sectorParentList.size()+1 ; s++) {
            for (EbcCompanySector sectorParent : sectorParentList) {
                ChildSectors childSectors=null;//子级对象
                List<ChildSectors> childSectorsList = new ArrayList<>();//子级列表
                for (EbcCompanySector cs : allSectors) {

                    if(cs.getId() == sectorParent.getId()){//父级id
                        SectorListVo sectorListVo = new SectorListVo();//父级对象
                        sectorListVo.setSectorId(cs.getId()).setSectorName(cs.getSectorName());
                        childSectorsList = new ArrayList<>();//子级列表
                        for (EbcCompanySector ps : allSectors) {
                            if(ps.getPid() == sectorParent.getId()){//子级id
                                childSectors = new ChildSectors();
                                childSectors.setChildSectorId(ps.getId()).setChildSectorName(ps.getSectorName());
                                childSectorsList.add(childSectors);
                            }
                        }
                        sectorListVo.setChildSector(childSectorsList);//存放子级列表
                        SectorList.add(sectorListVo);
                    }

                }
            }

//        }
        return JsonResult.success(SectorList);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public JsonResult addSectorParent(SectorInfoReq InfoReq) {
        // pid sectorName companyId
        EbcCompanySector sector = new EbcCompanySector();
        sector.setPid(0).setSectorName(InfoReq.getSectorName()).setCompanyId(InfoReq.getCompanyId());
        companySectorMapper.insert(sector);
        return JsonResult.success("添加一级部门 部门id:"+sector.getId());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public JsonResult addSectorChild(SectorInfoReq sectorInfoReq) {

        EbcCompanySector parentSector = companySectorMapper.selectById(sectorInfoReq.getSectorId());
        EbcCompanySector sector = new EbcCompanySector();
        if(parentSector == null){
            return JsonResult.fail("添加失败,请检查一级部门id :"+sectorInfoReq.getSectorId());
        }
        if(parentSector.getPid()==0){
            // parentId sectorName companyId
            sector.setPid(sectorInfoReq.getSectorId())//parentId
                    .setSectorName(sectorInfoReq.getSectorName()).setCompanyId(sectorInfoReq.getCompanyId());
            companySectorMapper.insert(sector);
        }else{
            return JsonResult.fail("添加失败,请检查一级部门id :"+sectorInfoReq.getSectorId());
        }

        return JsonResult.success("添加二级部门 部门id:"+sector.getId());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public JsonResult deleteSector(SectorInfoReq sectorInfoReq) {
        //如果当前部门下有员工存在 则失败
        JsonResult result=null;
        QueryWrapper<EbcCard> wrapper = new QueryWrapper<>();
        wrapper.eq("sector_id",sectorInfoReq.getSectorId());
        wrapper.eq("sector_child_id",sectorInfoReq.getSectorId());
        List<EbcCard> ebcCards = cardMapper.selectList(wrapper);
        if(ebcCards == null){
            // parentId
            companySectorMapper.deleteById(sectorInfoReq.getSectorId());
            result = JsonResult.success("删除成功 删除部门id:"+sectorInfoReq.getSectorId());
        }else{
            result = JsonResult.fail("删除失败,部门联系必须为空");
        }
        return result;
    }
}
